{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import date\n",
    "from gs_quant.common import PayReceive, Currency, BuySell\n",
    "from gs_quant.instrument import CDIndexOption\n",
    "from gs_quant.markets.portfolio import Portfolio\n",
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.markets import HistoricalPricingContext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we price a payer spread on the iTraxx Crossover OTR index\n",
    "\n",
    "We buy a XOVER call and sell an offsetting XOVER call with a higher strike"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Buy 3m XO call k=300\n",
    "long_leg = CDIndexOption(index_family='iTraxx Europe XOVER', strike=0.0300, option_type='Call', \n",
    "                         expiration_date='3m', termination_date='5y', buy_sell='Buy', name='Long_XOVER_Call')\n",
    "# Sell 3m XO call k=350\n",
    "short_leg = CDIndexOption(index_family='iTraxx Europe XOVER', strike=0.0350, option_type='Call', \n",
    "                          expiration_date='3m', termination_date='5y', buy_sell='Sell', name='Short_XOVER_Call')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "payer_spread = Portfolio((long_leg, short_leg))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with HistoricalPricingContext(date(2021, 4, 1), date(2021, 4, 30), show_progress=True):\n",
    "    prices = payer_spread.price()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prices.to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Plot both legs of the payer spraed as well as the overall PV\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "fig, ax = plt.subplots()\n",
    "ax.plot(prices.to_frame()['Long_XOVER_Call'], label='Buy leg, k=300')\n",
    "ax.plot(prices.to_frame()['Short_XOVER_Call'], label='Sell leg, k=350')\n",
    "ax.plot(prices.aggregate(), label='Overall cost')\n",
    "ax.set_xlabel('time')\n",
    "ax.set_ylabel('PV ($)')\n",
    "ax.set_title('PV over Time')\n",
    "ax.legend(bbox_to_anchor=(1, 0.5))\n",
    "fig.autofmt_xdate()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "prices.aggregate()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
